package com.whfc.fvs.service.xxl.job;

import com.whfc.XxlJobConfig;
import com.whfc.common.exception.BizException;
import com.whfc.common.vs.imou.entity.ImouDeviceInfo;
import com.whfc.fvs.dao.FvsDeviceMapper;
import com.whfc.fvs.dto.FvsDeviceDTO;
import com.whfc.fvs.enums.FvsDeviceStatus;
import com.whfc.fvs.enums.FvsDeviceType;
import com.whfc.fvs.enums.VsPlatform;
import com.whfc.fvs.service.FvsPlatformService;
import com.whfc.fvs.service.factroy.FvsPlatformServiceFactory;
import com.whfc.fvs.service.impl.FvsEzvizVsServiceImpl;
import com.whfc.fvs.service.impl.FvsImouVsServiceImpl;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * @author : qzexing
 * @version : 1.0
 * @date : 2020-09-25 14:41
 */
@Component
@ConditionalOnBean(XxlJobConfig.class)
public class FvsRefreshDeviceStateJob {


    @Autowired
    private FvsPlatformServiceFactory platformServiceFactory;

    @Autowired
    private FvsDeviceMapper fvsDeviceMapper;

    @XxlJob("refreshEzvizDeviceState")
    public void refreshEzvizDeviceState() {
        try {
            XxlJobHelper.log("更新海康设备状态...");
            updateDeviceState(VsPlatform.HIK_EZVIZ);
        } catch (Exception e) {
            XxlJobHelper.handleFail("更新海康设备状态失败 error:" + e.getMessage());
        }

    }

    @XxlJob("refreshImouDeviceState")
    public void refreshImouDeviceState() {
        try {
            XxlJobHelper.log("更新大华设备状态...");
            updateDeviceState(VsPlatform.DAHUA_IMOU);
        } catch (Exception e) {
            XxlJobHelper.handleFail("更新大华设备状态失败 error:" + e.getMessage());
        }
    }

    private void updateDeviceState(VsPlatform platform) throws BizException {
        XxlJobHelper.log("更新设备状态, platform:{}", platform);
        List<FvsDeviceDTO> deviceList = fvsDeviceMapper.selectByPlatform(platform.getValue());
        FvsPlatformService platFromService = platformServiceFactory.getFvsPlatFromService(platform);
        //存储在线设备ID
        List<Integer> onLineDeviceIdList = new ArrayList<>();
        //存储离线线设备ID
        List<Integer> offLineDeviceIdList = new ArrayList<>();
        for (FvsDeviceDTO fvsDeviceDTO : deviceList) {
            try {
                if (FvsDeviceType.DVR.getValue().equals(fvsDeviceDTO.getDeviceType())) {
                    // 跳过硬盘录像机
                    continue;
                }
                FvsDeviceStatus deviceStatus = FvsDeviceStatus.OFFLINE;
                if (VsPlatform.HIK_EZVIZ.equals(platform)) {
                    FvsEzvizVsServiceImpl fvsEzvizVsService = (FvsEzvizVsServiceImpl) platFromService;
                    deviceStatus = fvsEzvizVsService.getDeviceStatus(fvsDeviceDTO.getDeptId(), fvsDeviceDTO.getUsername());
                } else if (VsPlatform.DAHUA_IMOU.equals(platform)) {
                    FvsImouVsServiceImpl fvsImouVsService = (FvsImouVsServiceImpl) platFromService;
                    ImouDeviceInfo deviceInfo = fvsImouVsService.getDeviceInfo(fvsDeviceDTO.getDeptId(),
                            fvsDeviceDTO.getUsername(), fvsDeviceDTO.getChannelNo());
                    if (deviceInfo != null && 1 == deviceInfo.getStatus()) {
                        deviceStatus = FvsDeviceStatus.ONLINE;
                    }
                }
                if (FvsDeviceStatus.ONLINE.equals(deviceStatus)) {
                    onLineDeviceIdList.add(fvsDeviceDTO.getId());
                } else {
                    offLineDeviceIdList.add(fvsDeviceDTO.getId());
                }
            } catch (Exception ex) {
                XxlJobHelper.handleFail("萤石云刷新视频设备在线状态任务失败 error:{}" + ex.getMessage());
            }
        }
        batchUpdateDeviceStatus(onLineDeviceIdList, FvsDeviceStatus.ONLINE);
        batchUpdateDeviceStatus(offLineDeviceIdList, FvsDeviceStatus.OFFLINE);
    }

    private void batchUpdateDeviceStatus(List<Integer> deviceIdList, FvsDeviceStatus fvsDeviceStatus) throws BizException {
        XxlJobHelper.log("批量更新设备状态, deviceIdList:{}, fvsDeviceStatus:{}", deviceIdList, fvsDeviceStatus);
        if (deviceIdList == null || deviceIdList.isEmpty()) {
            return;
        }
        fvsDeviceMapper.batchUpdateDeviceStatus(deviceIdList, fvsDeviceStatus.getValue());
    }

}
